package com.hua.designpattern.behavioral.iterator;

import java.util.List;

public class ContainerList implements ContainerInterface {

	private List<String> elementData = null;

	public ContainerList(List<String> elementData) {
		super();
		this.elementData = elementData;
	}

	@Override
	public IteratorInterface getIterator() {
		return new ListIterator();
	}

	private class ListIterator implements IteratorInterface {

		private int index = elementData == null ? 0 : elementData.size();

		@Override
		public boolean hasNext() {
			return index > 0;
		}

		@Override
		public Object next() {
			return elementData.get(--index);
		}

	}

	@Override
	public String toString() {
		StringBuffer elementStr = new StringBuffer();
		for (String element : elementData) {
			elementStr.append(element + ",");
		}

		return super.toString() + "[" + elementStr + "]";
	}

}
